#!/usr/bin/env ruby
require  'socket'

def initialize
  	id_file = File.open("unit_test.id",File::CREAT|File::RDWR,0777)
    id_num = id_file.readlines
		sock = TCPSocket.open("127.0.0.1","34567")
#this is the test for marcro 
  if id_num.length == 0 then
		sock.print("\"marco\"")
		m = String.new("")
		m = sock.recv(256)
		arr = m.scan(/\"[^\"]+\"/)
		arr[1] = arr[1].scan(/[^\"]+/)
		@id = arr[1].to_s.to_i
		id_file.write(@id)
	else
		@id = id_num[0]
    sock.print(
	end	
	
	id_file.close
end

def Xmms_Plugin.run_plugin(r)
	flagged = 0
	nexted = 0
	message = String.new("")
	while(1)
	  	begin 
			if r.playlist? == true
				nexted = r.get_playlist_pos
				if (r.playing? == true and flagged == 0) then 	
					flagged = 1
# this is the test for increment	
					nexted = r.get_playlist_pos
					song_info = r.get_playlist_title
					s = song_info.scan(/[^-]+/)
					ss = s[1].scan(/[^:]+/)
					message <<"\"increment\""<<" "<<"\"#{@id}\""<<" "<<"\"#{r.get_playlist_file}\""<<" "<<"\"#{s[0]}\""<<" "<<"\"------\""<<" "<<"\"#{ss[1]}\""<<" "<< "\"------\""<< " "<< "\"------\"" <<" " << "\"------\""<< " "<< "\"------\"" <<" "<< "\"------\""
					sock = TCPSocket.open("127.0.0.1",@port.to_i)
					sock.print(message)
					m = String.new("")
					m = sock.recv(4)
					if m != "\"OK\"" then
						puts " omg_error"
					else 
						puts "ok\n"
					end
					message = String.new("")
#end of increment test
#beginning of the set test
					message <<"\"set\""<<" "<<"\"#{@id}\""<<" "<<"\"#{r.get_playlist_file}\""<<" "<<"\"#{s[0]}\""<<" "<<"\"------\""<<" "<<"\"#{ss[1]}\""<<" "<< "\"------\""<< " "<< "\"------\"" <<" " << "\"------\""<< " "<< "\"------\"" <<" "<< "\"------\""	
					sock = TCPSocket.open("127.0.0.1",@port.to_i)
					sock.print(message)
					m = String.new("")
					m = sock.recv(4)
					if m != "\"OK\"" then
						puts " omg_error"
					else 
						puts "ok\n"
					end
					message = String.new("")
#end of the set test
#get test
					message <<"\"get\""<<" "<<"\"#{r.get_playlist_file}\""
					sock = TCPSocket.open("127.0.0.1",@port.to_i)
					sock.print(message)
					m = String.new("")
					j = 1
					while (j != "") 
						j = sock.recv(10)
						m << j
					end 
					puts m 
					message = String.new("")
#end of get test
#new test
					message <<"\"new\""<<" "<<"\"#{@id}\""<<" "<<"\"#{r.get_playlist_file}\""<<" "<<"\"#{s[0]}\""<<" "<<"\"------\""<<" "<<"\"#{ss[1]}\""<<" "<< "\"------\""<< " "<< "\"------\"" <<" " << "\"------\""<< " "<< "\"------\"" <<" "<< "\"------\""	
					sock = TCPSocket.open("127.0.0.1",@port.to_i)
					sock.print(message)
					m = String.new("")
					j = 1
					while (j != "") 
						j = sock.recv(10)
						m << j
					end 
					puts m 
					message = String.new("")
#end of new test
#startup test
					message <<"\"startup\""<<" "<<"\"453\""
					sock = TCPSocket.open("127.0.0.1",@port.to_i)
					sock.print(message)
					m = String.new("")
					j = 1
					while (j != "") 
						j = sock.recv(10)
						m << j
					end 
					puts m 
					message = String.new("")
#end of startup test					
#ftp get test
					message <<"\"ftp\""<<" " << "\"get\""
					sock = TCPSocket.open("127.0.0.1",@port.to_i)
					sock.print(message)
					m = String.new("")
					j = 1
					while (j != "") 
						j = sock.recv(10)
						m << j
					end 
					puts m 
					message = String.new("")	
#ftp set test
					message <<"\"ftp\""<<" " << "\"set\""<<" " << "\"10\""<<" " << "\"10\""<<" " << "\"hello\""<<" " << "\"hello\""<<" " << "\"hello\""<<" " << "\"hello\""
					sock = TCPSocket.open("127.0.0.1",@port.to_i)
					sock.print(message)
					m = String.new("")
					j = 1
					while (j != "") 
						j = sock.recv(10)
						m << j
					end 
					puts m 
					message = String.new("")
					
				end
				if (r.playing? == false and r.paused? == false) then
					flagged = 0
				end
				if (nexted != r.get_playlist_pos) then 
					flagged = 0
				end
			end
		
		rescue Xmms::Error
			break
		end
	end
end

def  Xmms_Plugin.start(r)
	flag = 0
	while(1)
		if r.running? == true then
			if flag == 0 then
				configure()
				flag = 1
			end
			run_plugin(r)
		end
	end
end
end


r = Xmms::Remote.new
Xmms_Plugin.start(r)
